package com.heimo.babyage;

import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import com.heimo.babyage.core.LogHelper;
import com.heimo.babyage.core.PrefHelper;
import com.heimo.babyage.ui.dialogcolor.ColorDialog;
import com.larswerkman.colorpicker.ColorPicker.OnColorChangedListener;

public class PreferenceTextActivity extends BasicActivity {

	private static final String TAG = "PreferenceTextActivity";

	private Button prefPattern, prefTextColor, btnClose;
	private TextView textPatternLab, textColorLab, seekbarTextSizeLab;
	private SeekBar seekbarTextSize;
	private String textSizeLab = "", textPatternLab00, textPatternLab01,
			textPatternLab02, textPatternLab03, textPatternLab04;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pref_text);

		textPatternLab = (TextView) findViewById(R.id.title_text_pattern);
		prefPattern = (Button) findViewById(R.id.btn_text_pattern);

		textPatternLab00 = this.getString(R.string.lab_text_pattern_00);
		textPatternLab01 = this.getString(R.string.lab_text_pattern_01);
		textPatternLab02 = this.getString(R.string.lab_text_pattern_02);
		textPatternLab03 = this.getString(R.string.lab_text_pattern_03);
		textPatternLab04 = this.getString(R.string.lab_text_pattern_04);

		textColorLab = (TextView) findViewById(R.id.title_text_color);
		prefTextColor = (Button) findViewById(R.id.btn_text_color);

		seekbarTextSizeLab = (TextView) findViewById(R.id.title_text_size);
		seekbarTextSize = (SeekBar) findViewById(R.id.seekbar_text_size);
		textSizeLab = this.getString(R.string.lab_text_size);

		btnClose = (Button) findViewById(R.id.btn_close_text_pref);

		initData();
		initFontFace();
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		for (int i = 0; i < menu.size(); i++) {
			MenuItem item = menu.getItem(i);
			int id = item.getItemId();
			if (id == R.id.action_sponsor) {
				item.setVisible(true);
			}
		}
		return true;
	}

	private void initFontFace() {
		Typeface tf = Typeface.createFromAsset(getAssets(),
				"fonts/orbitron-bold.otf");
		if(tf!=null){
			textPatternLab.setTypeface(tf);
			prefPattern.setTypeface(tf);
			textColorLab.setTypeface(tf);
			prefTextColor.setTypeface(tf);
			seekbarTextSizeLab.setTypeface(tf);
			btnClose.setTypeface(tf);
		}
	}

	private void initData() {
		fnGetWidgetId();

		// init Text Pattern
		int pattern = PrefHelper.fnLoadTextPatternPref(this, mAppWidgetId);
		String[] items = getResources().getStringArray(
				R.array.string_array_tagetdt_pattern_name);
		prefPattern.setText(items[pattern]);

		// init Text Size
		int tSize = PrefHelper.fnLoadTextSizePref(this, mAppWidgetId);
		seekbarTextSize.setMax(24);
		seekbarTextSize.setProgress(tSize);
		seekbarTextSizeLab.setText(textSizeLab + tSize);
		fnSetSeekBarListener(seekbarTextSize);

		// init Text Color
		int color = PrefHelper.fnLoadTextColorPref(this, mAppWidgetId);
		prefTextColor.setBackgroundColor(color);
	}

	public void fnLoadTextPattern(View v) {
		final Context ctx = this;
		final String[] items = getResources().getStringArray(
				R.array.string_array_tagetdt_pattern_name);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				prefPattern.setText(items[item]);
				PrefHelper.fnSaveTextPatternPref(ctx, mAppWidgetId, item);
				fnUpdateView();
			}
		});
		AlertDialog alert = builder.create();
		alert.setTitle(R.string.lab_text_pattern);
		alert.show();
	}

	public void fnLoadColor(View v) {
		final Context ctx = this;
		int color = PrefHelper.fnLoadTextColorPref(this, mAppWidgetId);
		OnColorChangedListener ocl = new OnColorChangedListener() {

			@Override
			public void onColorChanged(int color) {
				prefTextColor.setBackgroundColor(color);
				PrefHelper.fnSaveTextColorPref(ctx, mAppWidgetId, color);
				fnUpdateView();
			}

		};
		// #88000000
		ColorDialog cd = new ColorDialog(this, color, ocl);
		cd.show();
	}

	private void fnGetWidgetId() {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		mAppWidgetId = -1;
		if (extras != null) {
			mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
		}
		LogHelper.log(TAG, "fnGetWidgetId : " + mAppWidgetId);
		// If they gave us an intent without the widget id, just bail.
		if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
			finish();
		}
	}

	public void fnClose(View v) {
		this.fnExit();
	}

	@Override
	public void onResume() {
		super.onResume();
		LogHelper.log(TAG, "onResume");
		initData();
	}

	@Override
	public void onNewIntent(Intent newIntent) {
		LogHelper.log(TAG, "onNewIntent");
		super.onNewIntent(newIntent);
		this.setIntent(newIntent);
	}

	private void fnSetSeekBarListener(SeekBar sb) {
		final Context ctx = this;
		sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (fromUser == true) {
					seekbarTextSizeLab.setText(textSizeLab + progress);
				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int p = seekBar.getProgress();
				seekbarTextSizeLab.setText(textSizeLab + p);
				PrefHelper.fnSaveTextSizePref(ctx, mAppWidgetId, p);
				fnUpdateView();
			}
		});
	}
	/*
	@Override
	protected void fnUpdateView() {
//		String eventName = this.getResources().getString(
//				R.string.widget_event_name);
//		Intent updateIntent = new Intent(this, BabyAgeWidgetProvider.class);
//		updateIntent.setAction(eventName);
//		sendBroadcast(updateIntent);

		RemoteViews rv = new RemoteViews(this.getPackageName(),
				R.layout.widget_layout_1);

		// Update Text Size
		int size = PrefHelper.fnLoadTextSizePref(this, mAppWidgetId);
		rv.setFloat(R.id.counter_text, "setTextSize", size);

		// Update Text Color
		int color = PrefHelper.fnLoadTextColorPref(this, mAppWidgetId);
		rv.setInt(R.id.counter_text, "setTextColor", color);

		AppWidgetManager awm = AppWidgetManager.getInstance(this);
		awm.updateAppWidget(
				new ComponentName(this, BabyAgeWidgetProvider.class), rv);

	}
	*/
}
